package ro.gateway.aida.sectors;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionForm;

import ro.gateway.aida.obj.AIDASector;
import ro.gateway.aida.utils.HttpUtils;



/**
 * Created by IntelliJ IDEA.
 * User: xblue
 * Date: Dec 15, 2003
 * Time: 10:24:10 AM
 * To change this template use Options | File Templates.
 */
public class SectorForm extends ActionForm
{
      public String action;
      public String event;
      public String lang;
      public AIDASector sector;
      public String[][] avail_langs;

      public SectorForm()
      {
            this.sector = new AIDASector();
      }

      public String[][] getAvail_langs()
      {
            return avail_langs;
      }

      public void setAvail_langs( String[][] avail_langs )
      {
            this.avail_langs = avail_langs;
      }

      public String getLang()
      {
            return lang;
      }

      public void setLang( String lang )
      {
      }

      public String getAction()
      {
            return action;
      }

      public void setAction( String action )
      {
            this.action = action;
      }

      public String getEvent()
      {
            return event;
      }

      public void setEvent( String event )
      {
            this.event = event;
      }

      public AIDASector getSector()
      {
            return sector;
      }

      public void setSector( AIDASector sector )
      {
            this.sector = sector;
      }

      public void clear()
      {
            this.sector = new AIDASector();
      }

      public int getRo_aida_code()
      {
            return sector.getRo_aida_code();
      }

      public void setRo_aida_code( int ro_aida_code )
      {
            this.sector.setRo_aida_code( ro_aida_code );
      }

      public void setDac5_code( String dac5_code )
      {
//            this.sector.setDac5_code( dac5_code );
      }

      public String getDac5_code()
      {
//            return sector.getDac5_code();
            return null;
      }

      public void setCrs_code( String crs_code )
      {
            this.sector.setCrs_code( crs_code );
      }

      public String getCrs_code()
      {
            return sector.getCrs_code();
      }

      public void setParent_ro_aida_code( int parent_ro_aida_code )
      {
            this.sector.setParent_ro_aida_code( parent_ro_aida_code );
      }

      public int getParent_ro_aida_code()
      {
            return sector.getParent_ro_aida_code();
      }

      public String getTitle()
      {
            String result = sector.getTitle( lang );
            if ( "N/A".equals( result ) )
            {
                  return null;
            }
            return result;
      }

      public void setTitle( String title )
      {
//            System.out.println( "set title:" + lang + "," + title );
//            sector.setTitle( lang, title );
      }

      public String getParentsName()
      {
            if ( sector.getParent() != null )
            {
                  return sector.getParent().getTitle( lang );
            }
            return null;
      }

      public void populate( HttpServletRequest request )
      {
            String newLang = HttpUtils.
                  getValidTrimedString( request, "lang", null );
            if ( newLang != null ) lang = newLang;
            String title = HttpUtils.getValidTrimedString( request, "title", null );
            if ( title != null && lang != null )
            {
                  title = title.toUpperCase();
                  sector.setTitle( lang, title );
            }

//            setDac5_code( HttpUtils.getValidTrimedString( request, "dac5_code", null ) );
            setCrs_code( HttpUtils.getValidTrimedString( request, "crs_code", null ) );
      }

      public String[] isValid()
      {
            ArrayList errors = new ArrayList();
            if ( getTitle() == null )
            {
                  errors.add( "title.req" );
            }

//            if ( getDac5_code() == null )
//            {
//                  errors.add( "dac5.req" );
//            }

            String result[] = null;
            if ( errors.size() > 0 )
            {
                  result = new String[ errors.size() ];
                  errors.toArray( result );
            }
            return result;
      }
}
